package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.Book;
import com.service.BookService;
import com.service.BookServiceImpl;

@WebServlet("/BookAddServlet")
public class BookAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Book book;
	BookService bookService;
       
    public BookAddServlet() {
        super();
        this.book = new Book();
        this.bookService = new BookServiceImpl();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("WEB-INF/views/addBook.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		String bookname = request.getParameter("bookname");
		String author = request.getParameter("author");
		String describe =request.getParameter("describe");
		double price = Double.parseDouble(request.getParameter("price"));
		long category_id = Long.parseLong(request.getParameter("category"));
		book = new Book(bookname, author, describe, price, category_id);
		if(bookService.addBook(book)){
			System.out.println(bookname+"\t"+author+"\t"+describe+"\t"+price+"\t"+category_id);
			response.sendRedirect("index");
		}
	}
}
